package com.zatech.cgnci.project.controller;

import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.base.utils.AreaUtils;
import com.zatech.cgnci.project.dto.AreaDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author EDY
 * @version 1.0
 * Create by 2024/12/18 15:41
 */
@RestController
@RequestMapping("/api/area")
@RequiredArgsConstructor
@Api(produces = "application/json", tags = "地址api接口文档")
public class CgnciAreaController {

    @GetMapping("/getArea")
    @ApiOperation("获取省/市/区")
    public List<AreaDTO> getArea(@ApiParam(value = "获取类型 1-省 2-市 3-区") @RequestParam @NotNull(message = "错误参数") Integer type,
                                 @ApiParam(value = "父级id") @RequestParam(required = false) String id) {
        List<AreaDTO> vo;
        switch (type) {
            case 1:
                vo = AreaUtils.getProvinceList(id);
                break;
            case 2:
                vo = AreaUtils.getCityList(id);
                break;
            case 3:
                vo = AreaUtils.getDistrictList(id);
                break;
            default:
                throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR);
        }
        return vo;
    }


    @GetMapping("/getAll")
    @ApiOperation("获取所有省市区")
    public List<AreaDTO> getAll() {
        List<AreaDTO> list = AreaUtils.getList(0, null);
        list.forEach(t1 -> {
            List<AreaDTO> list1 = AreaUtils.getList(1, t1.getCode());
            t1.setList(list1);
            list1.forEach(t2 -> {
                List<AreaDTO> list2 = AreaUtils.getList(2, t2.getCode());
                t2.setList(list2);
            });
        });
        return list;
    }

}
